package com.fzrskj.cloud.free.sys.utils;

import com.fzrskj.cloud.free.core.tool.CStrTool;
import com.fzrskj.cloud.free.sys.bean.SysNotifyTemplate;
import com.fzrskj.cloud.free.sys.dao.SysNotifyTemplateDao;
import ms.core.consts.SysErr;
import ms.core.object.CObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SysNotifyTemplateUtils extends CObject{
	/**
	 * 根据type查询数据
	 * @param type
	 * @return
	 */
	public List<SysNotifyTemplate> querEmailConfig(int type, int page, int pageSize){
		SysNotifyTemplateDao dao =new  SysNotifyTemplateDao();
		List<SysNotifyTemplate> querEmailConfig = dao.querEmailConfig(type,page, pageSize);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return querEmailConfig;
	}

	public int queryCount(int type){
		SysNotifyTemplateDao dao =new  SysNotifyTemplateDao();
		int row = dao.queryCount(type);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return row;
	}

	/**
	 * 根据ID查询表
	 * @param id
	 * @return
	 */
	public SysNotifyTemplate querEmailById(int id){
		SysNotifyTemplateDao dao =new  SysNotifyTemplateDao();
		SysNotifyTemplate sysNotifyRecord = dao.querEmailById(id);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return sysNotifyRecord;
	}


	/**
	 * 根据参数更新数据
	 * @param tempName
	 * @param title
	 * @param content
	 * @return
	 */
	public int uptemailconfig(String tempName,String title,String content){
		SysNotifyTemplateDao dao = new  SysNotifyTemplateDao();
		Map<String,Object> value = new HashMap<String, Object>();
		value.put("title", title);
		value.put("content", content);
		Map<String,Object> cdt = new HashMap<String, Object>();
		cdt.put("tempName", tempName);
		int ret = dao.uptemailconfig(value,cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;

	}

	/**to query notify template by msgTypeValue
	 * by lms 2016.08.30
	 * @param msgTypeValue
	 * @return null or SysNotifyTemplate Object
	 */
	public SysNotifyTemplate queryNotifyTemplate(String msgTypeValue){
		if (CStrTool.isBlankStr(msgTypeValue)){
			this.setErrCode(SysErr.SYS_PARAM_ERR);
			return null;
		}

		SysNotifyTemplateDao dao =new  SysNotifyTemplateDao();
		SysNotifyTemplate ret = dao.querySysNotifyTemplate(msgTypeValue);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	//邮件配置传过来的P参数 替换%号字符
	public String splitEmail(String p){
		String str = "";
		String[] array = p.split("仝仝");
		for(int i=0;i<array.length;i++){
			str+=array[i]+"%";
		}
		 str=str.substring(0,str.length()-1);
		 return str;
	} 

}